﻿using CSRedis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Micro.Core.FrameWork.Config;
using Microsoft.Extensions.Configuration;

namespace Micro.Core.FrameWork.Cache.Redis
{
    public class RedisCache : ICache
    {
        private readonly int _defaultTimeExpire;//默认过期秒数
        public RedisCache(IConfiguration configuration)
        {
            RedisHelper.Initialization(new CSRedisClient(configuration.GetSection("RedisConfig")["RedisConnectString"]));
            _defaultTimeExpire = Convert.ToInt32(configuration.GetSection("RedisConfig")["DefaultTimeExpire"]);
        }

        private void Dispose()
        {
            RedisHelper.Instance.Dispose();
            GC.SuppressFinalize(this);
        }
        /// <summary>
        /// 保存数据DB文件到硬盘
        /// </summary>
        private void Save()
        {
            RedisHelper.NodesServerManager.Save();

        }
        /// <summary>
        /// 异步保存数据DB文件到硬盘
        /// </summary>
        private void SaveAsync()
        {
            RedisHelper.NodesServerManager.SaveAsync();

        }
        /// <summary>
        /// 判断某个数据是否已经被缓存
        /// </summary>
        public bool Exist(string key)
        {
            try
            {
                var result = RedisHelper.Exists(key);
                return result;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 存储数据到redis
        /// </summary>
        public bool Set(string key, object value)
        {
            try
            {
                RedisHelper.Set(key, value, _defaultTimeExpire);
            }
            catch
            {
                return false;
            }
            return true;
        }
        /// <summary>
        /// 存储数据到redis
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="timeExpire"></param>
        /// <returns></returns>
        public bool Set(string key, string value, int timeExpire)
        {
            try
            {
                RedisHelper.Set(key, value, timeExpire);
            }
            catch
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 存储数据到hash表,默认24小时过期
        /// </summary>
        public bool Set<T>(string key, T t)
        {
            try
            {
                RedisHelper.Set(key, t, _defaultTimeExpire);
            }
            catch
            {
                return false;
            }
            return true;
        }
        /// <summary>
        /// 存储数据到hash表
        /// </summary>
        public bool Set<T>(string key, T t, int timeExpire)
        {
            try
            {
                RedisHelper.Set(key, t, timeExpire);
            }
            catch
            {
                return false;
            }
            return true;
        }
        /// <summary>
        /// 根据key获取数据
        /// </summary>
        public string Get(string key)
        {
            try
            {
                var value = RedisHelper.Get<string>(key);
                return value;
            }
            catch
            {
                return null;
            }
        }
        /// <summary>
        /// 存取任意类型的值
        /// </summary>
        public Task<T> Get<T>(string key)
        {
            var result = RedisHelper.GetAsync<T>(key);
            return result;
        }
        /// <summary>
        /// 刷新过期时间
        /// </summary>
        /// <param name="key"></param>
        /// <param name="timeExpire"></param>
        /// <returns></returns>
        public bool Expire(string key, int timeExpire)
        {
            return RedisHelper.Expire(key, timeExpire);
        }
    }
}
